package com.tanhua.server.service;

import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.api.AudioApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.dubbo.pojo.Audio;
import com.tanhua.dubbo.pojo.Video;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class AudioMQService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @DubboReference(version = "1.0.0")
    private AudioApi audioApi;


    /**
     * 发布语音
     *
     * @return
     */
    public Boolean audioMsg(String audioId) {
        return this.sendMsg(audioId);
    }

    /**
     * 发送语音操作相关的消息
     *
     * @param audioId
     * @param
     * @return
     */
    public Boolean sendMsg(String audioId) {
        try {
            Audio audio = this.audioApi.queryAudioById(audioId);
            //构建消息
            Map<String, Object> msg = new HashMap<>();
            msg.put("userId", UserThreadLocal.get());
            msg.put("date", System.currentTimeMillis());
            msg.put("audioId", audioId);
            msg.put("aid", audio.getAid());

            this.rocketMQTemplate.convertAndSend("taohua-audio", msg);
        } catch (Exception e) {
            log.error("发送消息失败! audioId = {}", audioId, e);
            return false;
        }
        return true;
    }
}
